package com.wish233.proxy;

import com.wish233.serializer.Serializer;
import com.wish233.serializer.factory.SerializerFactory;
import com.wish233.spi.SpiLoader;
import lombok.extern.slf4j.Slf4j;
import net.datafaker.Faker;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 服务代理（jdk动态代理）
 *
 * @author WHH
 */
@Slf4j
public class MockServiceProxy implements InvocationHandler {
    private static final Faker FAKER = new Faker();

    public static Object fakeReturnObj(Class<?> returnType) {
        if (returnType == null) {
            return null;
        }
        // 处理基本类型
        if (returnType.isPrimitive()) {
            if (returnType == int.class || returnType == Integer.class) {
                return FAKER.number().numberBetween(1, 100);
            } else if (returnType == long.class || returnType == Long.class) {
                return FAKER.number().randomNumber();
            } else if (returnType == double.class || returnType == Double.class) {
                return FAKER.number().randomDouble(2, 1, 1000);
            } else if (returnType == float.class || returnType == Float.class) {
                return (float) FAKER.number().randomDouble(2, 1, 100);
            } else if (returnType == boolean.class || returnType == Boolean.class) {
                return FAKER.bool().bool();
            } else if (returnType == char.class || returnType == Character.class) {
                return FAKER.lorem().character();
            } else if (returnType == byte.class || returnType == Byte.class) {
                return (byte) FAKER.number().numberBetween(0, 127);
            } else if (returnType == short.class || returnType == Short.class) {
                return (short) FAKER.number().numberBetween(0, 32767);
            }
        }

        // 处理包装类型
        if (returnType == Integer.class) {
            return FAKER.number().numberBetween(1, 100);
        } else if (returnType == Long.class) {
            return FAKER.number().randomNumber();
        } else if (returnType == Double.class) {
            return FAKER.number().randomDouble(2, 1, 1000);
        } else if (returnType == Float.class) {
            return (float) FAKER.number().randomDouble(2, 1, 100);
        } else if (returnType == Boolean.class) {
            return FAKER.bool().bool();
        } else if (returnType == Character.class) {
            return FAKER.lorem().character();
        } else if (returnType == Byte.class) {
            return (byte) FAKER.number().numberBetween(0, 127);
        } else if (returnType == Short.class) {
            return (short) FAKER.number().numberBetween(0, 32767);
        } else if (returnType == String.class) {
            return FAKER.lorem().sentence();
        } else if (returnType == BigDecimal.class) {
            return BigDecimal.valueOf(FAKER.number().randomDouble(2, 1, 1000));
        } else if (returnType == Date.class) {
            return FAKER.date().birthday();
        } else if (returnType == LocalDate.class) {
            return LocalDate.now().minusDays(FAKER.number().numberBetween(1, 365));
        } else if (returnType == LocalDateTime.class) {
            return LocalDateTime.now().minusHours(FAKER.number().numberBetween(1, 24));
        }

        // 处理集合类型
        if (List.class.isAssignableFrom(returnType)) {
            return Arrays.asList(fakeReturnObj(String.class), fakeReturnObj(String.class));
        } else if (Set.class.isAssignableFrom(returnType)) {
            return new HashSet<>(Arrays.asList(fakeReturnObj(String.class), fakeReturnObj(String.class)));
        } else if (Map.class.isAssignableFrom(returnType)) {
            Map<Object, Object> map = new HashMap<>();
            map.put(fakeReturnObj(String.class), fakeReturnObj(String.class));
            return map;
        }

        // 处理数组
        if (returnType.isArray()) {
            Class<?> componentType = returnType.getComponentType();
            Object array = Array.newInstance(componentType, 2);
            Array.set(array, 0, fakeReturnObj(componentType));
            Array.set(array, 1, fakeReturnObj(componentType));
            return array;
        }

        // 处理枚举
        if (returnType.isEnum()) {
            Object[] enumValues = returnType.getEnumConstants();
            return enumValues[ThreadLocalRandom.current().nextInt(enumValues.length)];
        }

        // 自定义对象 - 使用反射创建简单对象
        try {
            // 这里可以添加代码来填充对象的字段
            return returnType.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //获取业务类的返回类型
        Class<?> returnType = method.getReturnType();
        log.info("mock调用 返回类型{}", returnType.getName());
        //测试一下序列化
        SerializerFactory.getInstance("kryo");
        return fakeReturnObj(returnType);
    }

}
